package com.xy.Servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xy.dao.impl.PageResults;
import com.xy.service.impl.StockReceiveServiceImpl;
import com.xy.service.impl.StockServiceImpl;

/**
 * Servlet implementation class StockServlet
 */
@WebServlet("/stockServlet")
public class StockServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public StockServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		if ("list".equals(request.getParameter("status"))) {
			int currentPage = 0;
		      if (request.getParameter("goalpage") == null) {
		        currentPage = Integer.valueOf(request.getParameter("currentPage")) - 1;
		      } else
		        currentPage = Integer.valueOf(request.getParameter("goalpage")) - 1;

		      PageResults pageResults = new StockServiceImpl().listByPage(currentPage);

		      request.setAttribute("pageResults", pageResults);
		      request.setAttribute("status", "list");
		      request.setAttribute("list", null);

		      request.getRequestDispatcher("/pages/storage/storage003/stockpile.jsp").forward(request,
		          response);
		}else if ("query".equals(request.getParameter("status"))) {
			int currentPage = 0;
			if (request.getParameter("goalpage") == null) {
				currentPage = Integer.valueOf(request.getParameter("currentPage")) - 1;
			} else
				currentPage = Integer.valueOf(request.getParameter("goalpage")) - 1;

			String cell = null;
			String textBook = null;
			String list = null;

			if (request.getParameter("list") == null) {
				cell = request.getParameter("cell") == null ? "%" : request.getParameter("cell");
				textBook = request.getParameter("textBook") == null ? "%" : request.getParameter("textBook");
				list = cell + "," + textBook;
			} else {
				String s = new String(request.getParameter("list").getBytes("iso-8859-1"), "utf-8");
				String [] ss = s.split(",");
				for(int i=0;i<ss.length;i++) {
					cell = ss[0];
					textBook = ss[1];
				}
				list = cell + "," + textBook;
			}

			PageResults pageResults = new StockServiceImpl().query(cell,textBook, currentPage);

			request.setAttribute("pageResults", pageResults);
			request.setAttribute("status", "query");
			request.setAttribute("list", list);

			if("pandian".equals(request.getParameter("pre"))) {
				request.getRequestDispatcher("/pages/storage/storage004/pandian.jsp").forward(request,
				          response);
			}else
				request.getRequestDispatcher("/pages/storage/storage003/stockpile.jsp").forward(request,
			          response);
		}
	}

}
